﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Const;
using Entitas;
using UnityEngine;

public class EliminateSameColorSystem : ReactiveSystem<GameEntity>
{
    private Contexts _contexts;
    public EliminateSameColorSystem(Contexts context) : base(context.game)
    {
        _contexts = context;
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsDestroy);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsItemEffectState
               && entity.componentsItemEffectState.EffectState == ItemEffectName.ELIMINATE_SAME_COLOR;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in entities)
        {
            var gameBoard = _contexts.game.componentsGameBoardComponents;
            string colorName = entity.componentsLoadPrefab.Path;
            GameEntity temp;
            for (int column = 0; column < gameBoard.columns; column++)
            {
                for (int raw = 0; raw < gameBoard.raws; raw++)
                {
                    temp = _contexts.game
                        .GetEntitiesWithComponentsItemIndex(new customData(column, raw))
                        .FirstOrDefault(u => u.componentsLoadPrefab.Path == colorName);
                    if (temp != null)
                        temp.isComponentsDestroy = true;
                }
            }
            
        }
    }
}
